//
//  EditViewController.m
//  SoundPill
//
//  Created by 宋旭东®Des on 15/1/21.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "EditViewController.h"
#import "JSONKit.h"
#import "LoadingAnimation.h"
#import <math.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "imageVPickController.h"
//#import "TWPhotoPickerController.h"
//#import "VPImageCropperViewController.h"
#import "JCAlertView.h"
#import "LCActionSheet.h"
@interface EditViewController ()<AVAudioPlayerDelegate,UITextViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    CGRect markRect;
    BOOL personMark;
    NSString *textTime;
    int textnum;
    int num;
    int isCover;
    int isEdited;
    int isAddImg;
    UIView *black_view;
    NSString *audioDuration;
    float audioDurationFloat;
    UIButton *back;
    ALAssetsLibrary *lib;
}
@property (strong, nonatomic) NSMutableArray *imageArray;
@property (strong, nonatomic) NSMutableArray *photoArray;
@property (strong, nonatomic) NSMutableArray *imageTimeArray;
@property (strong, nonatomic) NSMutableArray *textArray;
//@property (strong, nonatomic) NSMutableArray *personArray;
@property (strong, nonatomic) NSMutableArray *audioWaveArray;
@property (strong, nonatomic) NSDictionary *textDic;
@property (strong, nonatomic) NSDictionary *personDic;
@property (strong, nonatomic) NSURL *audioURL;
@property (strong, nonatomic) NSTimer *playtimer;
@property (strong, nonatomic) NSNumber *timestamp;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIButton *playButton;
@property (strong, nonatomic) UIButton *publish;
@property (strong, nonatomic) UIView *waveMask;
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@property (strong, nonatomic) Edit_addTextView *textView;
@property (strong, nonatomic) Edit_TextView *showTextView;
@property (strong, nonatomic) UIButton *addText;
@property (strong, nonatomic) UIButton *editButton;
@property (strong, nonatomic) UIButton *addImage;
@property (strong, nonatomic) edit_showPerson *showPerson;
@property (strong, nonatomic) editAudioWave *AudioWave;
@property (strong, nonatomic) UIBarButtonItem *publishItem;
@property (strong, nonatomic) UILabel *currentTimeLabel;
@property (assign, nonatomic) float interval;
@property (assign, nonatomic) BOOL textState; //no 表示添加, yes 表示修改
@property (assign, nonatomic) int xiabiao; //当前文字在数组中的下标
//草稿箱
@property (strong, nonatomic) NSString *draftPath;
@property (strong, nonatomic) NSString *edit_workSpace;
@property (nonatomic ,strong) NSString *edit_workTime;//音频创建时间
@property (strong, nonatomic) NSArray *waveformComplete;
@property (strong, nonatomic) NSMutableArray *draftArray;
@property (strong, nonatomic) NSArray *draftImage;
@property (strong, nonatomic) NSMutableArray *markXArray;
@end

@implementation EditViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.imageArray = [[NSMutableArray alloc] init];
        self.photoArray = [[NSMutableArray alloc] init];
        self.imageTimeArray = [[NSMutableArray alloc] init];
        self.textArray = [[NSMutableArray alloc] init];
//        self.personArray = [[NSMutableArray alloc] init];
        self.audioWaveArray = [[NSMutableArray alloc] init];
        self.timestamp = [[NSNumber alloc] init];
        personMark = NO;
        num = 0;
        textnum = 0;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioProgress:) name:@"audioProgress" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerPause:) name:@"pause" object:nil];
        self.hidesBottomBarWhenPushed = YES;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteImage:) name:@"editImageText" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFirstImage:) name:@"changeFirstImage" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteText:) name:@"deleteText" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(replaceText:) name:@"replaceText" object:nil];
    }
    return self;
}
- (void)deleteImage:(NSNotification *)notification{
//    NSArray *array = notification.object;
//    [_personArray addObjectsFromArray:array];
    NSIndexPath *path = notification.object;
    NSDictionary *dic = _imageArray[path.row];
    NSString *time = [dic valueForKey:@"timeline"];
    UIImageView *blueMask = (UIImageView *)[_waveMask viewWithTag:50 +  time.floatValue * 100];
    [_photoArray removeObjectAtIndex:path.row];
    [_imageArray removeObjectAtIndex:path.row];
    [blueMask removeFromSuperview];
    [self changeShowImage];
}
- (void)changeFirstImage:(NSNotification *)notification{
    UIImage *asset = notification.object;
//    CGImageRef posterImageRef=[asset.defaultRepresentation fullScreenImage];
//    UIImage *posterImage=[UIImage imageWithCGImage:posterImageRef];
    NSString *timeline = @"0.000000";
    NSDictionary *imgDic = [NSDictionary dictionaryWithObjectsAndKeys:asset,@"image", timeline, @"timeline", nil];
    [_photoArray replaceObjectAtIndex:0 withObject:asset];
    [_imageArray replaceObjectAtIndex:0 withObject:imgDic];
    [self changeShowImage];
}
- (void)changeShowImage
{
    num = 0;
    if (_imageArray.count > 0) {
        for (int i = num; i < _imageArray.count; i++) {
            NSString *time = [_imageArray[i] valueForKey:@"timeline"];
            if (_audioPlayer.currentTime/_audioPlayer.duration >= time.floatValue) {
                _imageView.image = [_imageArray[i] valueForKey:@"image"];
                num = i;
            }
        }
    }
}
- (void)deleteText:(NSNotification *)notification
{
    NSIndexPath *path = notification.object;
    NSString *timeline = [_textArray[path.row] valueForKey:@"timeline"];
    UIImageView *blackMask = (UIImageView *)[_waveMask viewWithTag:150 + timeline.floatValue * 1000];
    [blackMask removeFromSuperview];
    [_textArray removeObjectAtIndex:path.row];
    [self changeShowText];
}
- (void)replaceText:(NSNotification *)notification
{
    NSDictionary *dic = notification.object;
    NSNumber *indexPath = [dic valueForKey:@"index"];
    NSDictionary *dict = [dic objectForKey:@"newText"];
    [_textArray replaceObjectAtIndex:indexPath.integerValue withObject:dict];
//    NSLog(@"%@", _textArray);
    [self changeShowText];
}
- (void)changeShowText
{
    textnum = 0;
    if (_textArray.count != 0) {
        for (int i = textnum; i < _textArray.count; i++) {
            NSString *time = [_textArray[i] valueForKey:@"timeline"];
            if (_audioPlayer.currentTime / _audioPlayer.duration >= time.floatValue) {
                _showTextView.hidden = NO;
                [self showTextInImage:[_textArray[i] valueForKey:@"content"]];
                textnum = i;
            }
        }
    } else {
        _showTextView.hidden = YES;
    }
}
#pragma mark -
- (void)getImageArray:(NSMutableArray *)array auidoURL:(NSURL *)audioURL waveArray:(NSArray *)waveArray workSpace:(NSString *)workSpace workTime:(NSString *)workTime textArray:(NSArray *)textArray audioDurationStr:(NSString *)durationStr audioDurationFloat:(float)DurationFloat{
    self.edit_workSpace = workSpace;
    self.edit_workTime = workTime;
    self.waveformComplete = [NSArray arrayWithArray:waveArray];
    audioDuration = durationStr;
    audioDurationFloat = DurationFloat;
    isCover = 1;
    [self createView];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_playButton setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width)];
    [_playButton setBackgroundImage:[UIImage imageNamed:@"edit_addcover_button"] forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(playStart:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_playButton];
    self.audioURL = audioURL;
    //计算有波形 view 需要显示多少数据
    float count = (self.view.frame.size.width - 10) / 2;
    float waveInterval = [waveArray count] / count;
    int numm = ceilf(waveInterval);
    for (int i = 0; i < [waveArray count];) {
        if (numm <= 1) {
            [_audioWaveArray addObjectsFromArray:waveArray];
            break;
        } else {
            [_audioWaveArray addObject:waveArray[i]];
            i+=numm;
        }
    }
    if (_audioWaveArray.count * 2 < self.view.frame.size.width - 10) {
        [self.AudioWave setFrame:CGRectMake(5, self.view.frame.size.height / 2 + 44, _audioWaveArray.count * 2, self.view.frame.size.width / 8)];
//        _AudioWave.backgroundColor = [UIColor redColor];
    }
    if (_isDraft.boolValue == YES) {
        [self getDataWithDrift];
    }
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [session setActive:YES error:nil];

    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_audioURL error:nil];
    _audioPlayer.delegate = self;
    _audioPlayer.numberOfLoops = 0;
    [_audioPlayer prepareToPlay];
    [self createAudioLabel];
}
- (void)getDataWithDrift
{
    
    NSFileManager *manager =[NSFileManager defaultManager];
//    NSArray *fileList = [[NSArray alloc] init];
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
//    fileList = [manager contentsOfDirectoryAtPath:_edit_workSpace error:&error];
//    NSLog(@"%@", fileList);
    if ([manager fileExistsAtPath:[_edit_workSpace stringByAppendingPathComponent:@"imagetimeline"]]) {
        isCover = 0;
        [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
        NSArray *imageTimeline =[NSArray arrayWithContentsOfFile:[_edit_workSpace stringByAppendingPathComponent:@"imagetimeline"]];
        for (int i = 0; i < imageTimeline.count; i++) {
            NSString *imageString = [NSString stringWithFormat:@"full%d.png", i];
            UIImage *image = [UIImage imageWithContentsOfFile:[_edit_workSpace stringByAppendingPathComponent:imageString]];
            if (i == 0) {
                _imageView.image = image;
            }
            NSString *imagetime = imageTimeline[i];
            NSDictionary *imgDic = [NSDictionary dictionaryWithObjectsAndKeys:image,@"image", imagetime, @"timeline", nil];
            [_imageArray addObject:imgDic];
            [_photoArray addObject:image];
            UIImageView *imageMask = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Edit_character_mark"]];
            imageMask.tag = 50 + textTime.floatValue * 100;
//            NSLog(@"%@", imageMask);
//            CGRect imagemarkRect;

            if ([self determineTheTimePoint:_textArray]) {
                imageMask.frame = CGRectMake(_AudioWave.frame.size.width * imagetime.floatValue, 5, 12, 12);
//                imagemarkRect = imageMask.frame;
            } else {
                imageMask.frame = CGRectMake(_AudioWave.frame.size.width * imagetime.floatValue, 12, 12, 12);
//                imagemarkRect = imageMask.frame;
            }
            [_waveMask addSubview:imageMask];
        }
    }
    if ([manager fileExistsAtPath:[_edit_workSpace stringByAppendingPathComponent:@"text"]]) {
        NSArray *text = [NSArray arrayWithContentsOfFile:[_edit_workSpace stringByAppendingPathComponent:@"text"]];
        [_textArray addObjectsFromArray:text];
        for (NSDictionary *dic in _textArray) {
            NSString *driftTextTime = [dic valueForKey:@"timeline"];
            UIImageView *markImage = [[UIImageView alloc] initWithFrame:CGRectZero];
            if ([self determineTheTimePoint:_imageArray]) {
                markImage.frame = CGRectMake(_AudioWave.frame.size.width * driftTextTime.floatValue, 5, 12, 12);
                markRect = markImage.frame;
            } else {
                markImage.frame = CGRectMake(_AudioWave.frame.size.width * driftTextTime.floatValue, 12, 12, 12);
                markRect = markImage.frame;
            }
            markImage.tag = 150 + driftTextTime.floatValue * 1000;
            markImage.image = [UIImage imageNamed:@"Edit_text_mark"];
            [_waveMask addSubview:markImage];
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.publish = [UIButton buttonWithType:UIButtonTypeCustom];
    [_publish setFrame:CGRectMake(0, 0, 40, 30)];
    [_publish setImage:[UIImage imageNamed:@"done"] forState:UIControlStateNormal];
    [_publish addTarget:self action:@selector(goToPublishController) forControlEvents:UIControlEventTouchUpInside];
    self.publishItem = [[UIBarButtonItem alloc] initWithCustomView:_publish];
    self.navigationItem.rightBarButtonItem = _publishItem;
    if (_isDraft.boolValue == YES) {
        _publish.hidden = NO;
    } else {
        _publish.hidden = YES;
    }
    back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setFrame:CGRectMake(0, 0, 30, 30)];
    [back setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backItem;
    [self createButtonAndProgress];
}
- (void)createView{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width)];
    _imageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_imageView];
    [_imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    _imageView.contentMode =  UIViewContentModeScaleAspectFill;
    _imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _imageView.clipsToBounds  = YES;
    
    //文字输入框
    self.textView = [[Edit_addTextView alloc] initWithFrame:CGRectMake(0, _imageView.frame.size.height / 3 * 2 - 20, self.view.frame.size.width, 40)];
    _textView.backgroundColor = [UIColor clearColor];
    _textView.textView.delegate = self;
    [_textView.cancel addTarget:self action:@selector(cancelLeaveEdit) forControlEvents:UIControlEventTouchUpInside];
    [_textView.done addTarget:self action:@selector(leaveEditMode) forControlEvents:UIControlEventTouchUpInside];
    _textView.hidden = YES;
    [self.view addSubview:_textView];
    //文字显示
    self.showTextView = [[Edit_TextView alloc] initWithFrame:CGRectZero];
    _showTextView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_showTextView];
    _showTextView.hidden = YES;
    //人名显示
//    self.showPerson = [[edit_showPerson alloc] init];
//    _showPerson.frame = CGRectZero;
//    _showPerson.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.500];
////    [_showPerson addTarget:self action:@selector(deletePerson:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:_showPerson];
    //音频进度条
    self.AudioWave = [[editAudioWave alloc] initWithFrame:CGRectMake(5, self.view.frame.size.height / 2 + 44, self.view.frame.size.width - 10, self.view.frame.size.width / 8)];
    [_AudioWave.editAudioWaveAll getAudioWaveArray:_audioWaveArray offsetArray:nil];
    [_AudioWave getAudioWaveArray:_audioWaveArray];
    _AudioWave.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_AudioWave];
    //波形图蒙版
    self.waveMask = [[UIView alloc] initWithFrame:CGRectMake(0,  _AudioWave.bounds.size.height / 2, _AudioWave.bounds.size.width, _AudioWave.bounds.size.height / 2)];
    _waveMask.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.000];
    [self.AudioWave addSubview:_waveMask];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, _imageView.frame.origin.y + _imageView.frame.size.height + 70, self.view.frame.size.width, 1)];
    view.backgroundColor = [UIColor colorWithWhite:0.972 alpha:1.000];
    [self.view addSubview:view];
    if (_isDraft.boolValue == NO || ![[NSFileManager defaultManager] fileExistsAtPath:[_edit_workSpace stringByAppendingPathComponent:@"imagetimeline"]]) {
        black_view = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.width, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.width)];
        black_view.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.500];
        [self.view addSubview:black_view];
    }
}
- (void)createButtonAndProgress{
    //    添加文字
    self.addText = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addText setFrame:CGRectMake(0, self.view.frame.size.width + 70, self.view.frame.size.width / 3, self.view.frame.size.width / 3)];
    [_addText setImage:[UIImage imageNamed:@"edit_addText_button"] forState:UIControlStateNormal];
    [_addText addTarget:self action:@selector(addText:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_addText];
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, _addText.frame.size.height - _addText.frame.size.height / 3, _addText.frame.size.width, _addText.frame.size.height / 3)];
    text.text = @"添加字幕";
    text.textAlignment = NSTextAlignmentCenter;
    text.textColor = [UIColor blackColor];
    text.font = [UIFont systemFontOfSize:12];
    [_addText addSubview:text];
//添加图片
    self.addImage = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addImage setFrame:CGRectMake(self.view.frame.size.width / 3, self.view.frame.size.width + 70, self.addText.bounds.size.width, self.addText.bounds.size.height)];
    [_addImage setImage:[UIImage imageNamed:@"edit_addImage_button"] forState:UIControlStateNormal];
    [_addImage addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_addImage];
    UILabel *image = [[UILabel alloc] initWithFrame:CGRectMake(0, _addImage.frame.size.height - _addImage.frame.size.height / 3, _addImage.frame.size.width, _addImage.frame.size.height / 3)];
    image.text = @"添加插图";
    image.textAlignment = NSTextAlignmentCenter;
    image.textColor = [UIColor colorWithRed:0.184f green:0.216f blue:1.000f alpha:1.00f];
    image.font = [UIFont systemFontOfSize:12];
    [_addImage addSubview:image];
//@人
    self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_editButton setFrame:CGRectMake(self.view.frame.size.width / 3 * 2, self.view.frame.size.width + 70, self.addText.bounds.size.width , self.addText.bounds.size.height)];
    [_editButton setImage:[UIImage imageNamed:@"edit_changeImgTxt_button"] forState:UIControlStateNormal];
    [_editButton addTarget:self action:@selector(editWorkImageAndText:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_editButton];
    UILabel *edit = [[UILabel alloc] initWithFrame:CGRectMake(0, _editButton.frame.size.height - _editButton.frame.size.height / 3, _editButton.frame.size.width, _editButton.frame.size.height / 3)];
    edit.text = @"编辑列表";
    edit.textAlignment = NSTextAlignmentCenter;
    edit.textColor = [UIColor colorWithRed:0.600f green:0.600f blue:0.600f alpha:1.00f];
    edit.font = [UIFont systemFontOfSize:12];
    [_editButton addSubview:edit];
}
//#pragma mark -
//#pragma mark 删除人
//- (void)deletePerson:(UIButton *)sender
//{
//    NSString *str =  _showPerson.showPerson.titleLabel.text;
//    for (NSDictionary *dic in _personArray) {
//        NSString *name = [dic valueForKey:@"name"];
//        if ([name isEqualToString:str]) {
//            NSString *time = [dic valueForKey:@"timeline"];
//            int xiabiao = time.floatValue * 1000;
//            UIView *imageView = [_waveMask viewWithTag:xiabiao];
//            [imageView removeFromSuperview];
//            [_personArray removeObject:dic];
//            _showPerson.hidden = YES;
//            break;
//        }
//    }
//}
#pragma mark
#pragma makr 音频播放时长
- (void)createAudioLabel{
    UIView *titleview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    titleview.backgroundColor = [UIColor clearColor];
    self.navigationItem.titleView = titleview;
    UILabel *audioTime = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 60, 30)];
    audioTime.textColor = [UIColor blackColor];
    audioTime.textAlignment = NSTextAlignmentLeft;
    audioTime.font = [UIFont fontWithName:@"Arial" size:18];
//    int m = _aud / 60;
//    int s = ((int) _audioPlayer.duration) % 60;
    audioTime.text = audioDuration;
    [titleview addSubview:audioTime];
    
    self.currentTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 ,60, 30)];
    _currentTimeLabel.textColor = [UIColor blackColor];
    _currentTimeLabel.textAlignment = NSTextAlignmentRight;
    _currentTimeLabel.font = [UIFont fontWithName:@"Arial" size:18];
    _currentTimeLabel.text = [NSString stringWithFormat:@"00'00\"/"];
    [titleview addSubview:_currentTimeLabel];
}
- (void)playStart:(UIButton *)sender{
    if (isCover == 1) {
        [self addImage:_addImage];
//        [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
    } else {
        if ([_audioPlayer isPlaying]) {
            [_audioPlayer pause];
            if (_playtimer) {
                [_playtimer setFireDate:[NSDate distantFuture]];
            }
            [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
        } else {
            [_audioPlayer play];
            if (_playtimer) {
                [_playtimer setFireDate:[NSDate distantPast]];
            } else {
                self.playtimer = [NSTimer scheduledTimerWithTimeInterval:0.08 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];
            }
            [_playButton setBackgroundImage:nil forState:UIControlStateNormal];
        }
    }
}
- (void)playProgress{
    float value = _audioPlayer.currentTime / _audioPlayer.duration;
    [_AudioWave getAudioProgress:value];
    if (_imageArray.count != 0) {
        for (int i = num; i < _imageArray.count; i++) {
            NSString *time = [_imageArray[i] valueForKey:@"timeline"];
            if (value >= time.floatValue - 0.005) {
                CATransition *transition = [CATransition animation];
                transition.duration = 0.5;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.type = kCATransitionFade;
                [_imageView.layer addAnimation:transition forKey:nil];
                _imageView.image = [_imageArray[i] valueForKey:@"image"];
                num = i;
            }
        }
    }
    if (_textArray.count != 0) {
        for (int i = textnum; i < _textArray.count; i++) {
            NSString *time = [_textArray[i] valueForKey:@"timeline"];
            if (value >= time.floatValue - 0.005) {
                CATransition *transition = [CATransition animation];
                transition.duration = 0.5;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.type = kCATransitionFade;
                [_showTextView.layer addAnimation:transition forKey:nil];
                _showTextView.hidden = NO;
                [self showTextInImage:[_textArray[i] valueForKey:@"content"]];
                textnum = i;
            }
        }
    }
    _currentTimeLabel.text = [NSString stringWithFormat:@"%@", [self getAudioTime]];
}
#pragma mark -
#pragma mark 导航栏按钮方法
- (void)goToPublishController
{
    if (_photoArray.count > 0) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self savePhotoAndThumb];
        });
    }
    NSNumber *audioSecond = [NSNumber numberWithFloat:audioDurationFloat];
    TagViewController *tag = [[TagViewController alloc] init];
    if (self.isDraft.boolValue == YES) {
        tag.isDrift = [NSNumber numberWithBool:YES];
    } else {
        tag.isDrift = [NSNumber numberWithBool:NO];
    }
    //文字数组 @人数组 音频时长
    [tag getimageArray:_imageArray audioSecond:audioSecond waveArray:_waveformComplete text:_textArray person:nil workTime:_edit_workTime];
    [_audioPlayer stop];
    [_playtimer invalidate];
    _playtimer = nil;
    [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
    [self.navigationController pushViewController:tag animated:YES];
    
}
- (void)back:(UIButton *)sender
{
    if (_photoArray.count > 0) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self savePhotoAndThumb];
        });
    }
    [_audioPlayer stop];
    [_playtimer invalidate];
    _playtimer = nil;
    [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma makr 添加人物
- (void)editWorkImageAndText:(UIButton *)sender
{
    if ([_audioPlayer isPlaying]) {
        [_audioPlayer pause];
        [_playtimer setFireDate:[NSDate distantFuture]];
        [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
    }
    EditImageTextViewController *editImageText = [[EditImageTextViewController alloc] init];
    [editImageText getImageArray:_imageArray textArray:_textArray];
    UINavigationController *editImageTextNav = [[UINavigationController alloc] initWithRootViewController:editImageText];
    [self presentViewController:editImageTextNav animated:YES completion:nil];
    isEdited = 1;
}
#pragma mark -
#pragma mark 添加图片
- (void)addImage:(UIButton *)sender{
//    isAddImg = 1;
    lib = [[ALAssetsLibrary alloc] init];
    if (_photoArray.count < 10) {
        if ([self determineTheTimePoint:_imageArray]) {
            if (isCover == 0) {
                [_audioPlayer pause];
                [_playtimer setFireDate:[NSDate distantFuture]];
                [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
            } else {
                
            }
            LCActionSheet *imageSourceSheet = [LCActionSheet sheetWithTitle:@"请选择图片来源" buttonTitles:@[@"拍照",@"相册"] redButtonIndex:-1 clicked:^(NSInteger buttonIndex) {
                imageVPickController *imagePicker = [[imageVPickController alloc] init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = YES;
                if (buttonIndex == 0) {
                    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                   
                } else if (buttonIndex == 1) {
                    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    // [self presentModalViewController:imagePicker animated:YES];
                }
                [self presentViewController:imagePicker animated:YES completion:nil];
            }];
            [imageSourceSheet show];
            
        } else {
            [JCAlertView showOneButtonWithTitle:@"提示" Message:@"在同一时间点只能添加一张图片" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
                
            }];
        }
    } else {
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"最多添加10张图片" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];
    }
}
- (void)getSelectedPhoto:(UIImage *)selectImage{
    if (selectImage) {
        [self.photoArray addObject:selectImage];
        textTime = [NSString stringWithFormat:@"%f", (_audioPlayer.currentTime / _audioPlayer.duration)];
        NSDictionary *imgDic = [NSDictionary dictionaryWithObjectsAndKeys:selectImage,@"image", textTime, @"timeline", nil];
        if (_imageArray.count > 0) {
            NSString *text_last_t = [[_imageArray lastObject] valueForKey:@"timeline"];
            if (textTime.floatValue > text_last_t.floatValue) {
                [_imageArray addObject:imgDic];
            } else {
                for (int j = 0; j < _imageArray.count; j++) {
                    NSString *text_t = [_imageArray[j] valueForKey:@"timeline"];
                    if (textTime.floatValue < text_t.floatValue) {
                        [_imageArray insertObject:imgDic atIndex:j];
                        break;
                    }
                }
            }
        } else {
            [_imageArray addObject:imgDic];
        }
        _imageView.image = [[_imageArray lastObject] valueForKey:@"image"];
        UIImageView *imageMask = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Edit_character_mark"]];
        imageMask.tag = 50 + textTime.floatValue * 100;
        CGRect imagemarkRect;
        if ([self determineTheTimePoint:_textArray]) {
            imageMask.frame = CGRectMake(_AudioWave.offsetNum.floatValue, 5, 12, 12);
            imagemarkRect = imageMask.frame;
        } else {
            imageMask.frame = CGRectMake(_AudioWave.offsetNum.floatValue, 12, 12, 12);
            imagemarkRect = imageMask.frame;
        }
        [_waveMask addSubview:imageMask];
        _publish.userInteractionEnabled = NO;
        back.userInteractionEnabled = NO;
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            imageMask.frame = CGRectMake(imageMask.frame.origin.x  - 6, imageMask.frame.origin.y - 6 , 24, 24);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                imageMask.frame = imagemarkRect;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.2 animations:^{
                    imageMask.frame = CGRectMake(imageMask.frame.origin .x - 4 , imageMask.frame.origin.y - 4, 20, 20);
                } completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.4 animations:^{
                        imageMask.frame = imagemarkRect;
                    } completion:^(BOOL finished) {
                        [UIView animateWithDuration:0.2 animations:^{
                            imageMask.frame = CGRectMake(imageMask.frame.origin.x - 2, imageMask.frame.origin.y - 2, 16, 16);
                        } completion:^(BOOL finished) {
                            [UIView animateWithDuration:0.4 animations:^{
                                imageMask.frame = imagemarkRect;
                            } completion:^(BOOL finished) {
                                if (_textView.hidden == YES) {
                                    [_audioPlayer play];
                                    if (_playtimer) {
                                        [_playtimer setFireDate:[NSDate distantPast]];
                                    } else {
                                        self.playtimer = [NSTimer scheduledTimerWithTimeInterval:0.08 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];
                                    }
                                    [_playButton setBackgroundImage:nil forState:UIControlStateNormal];
                                }
                                _publish.userInteractionEnabled = YES;
                                        back.userInteractionEnabled = YES;
                            }];
                        }];
                    }];
                }];
            }];
        }];
    }
    if (isCover == 1) {
        _waveMask.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.490];
        [black_view removeFromSuperview];
        [_playButton setBackgroundImage:nil forState:UIControlStateNormal];
        _publish.hidden = NO;
        isCover = 0;
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    editedImage = [editedImage fixOrientation];
    [self getSelectedPhoto:editedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -
#pragma mark 文字输入
- (void)addText:(UIButton *)sender{
    
    [_audioPlayer pause];
    [_playtimer setFireDate:[NSDate distantFuture]];
    _playButton.userInteractionEnabled = NO;
    [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
    if (_textArray.count > 0) {
        if (_textArray.count < 20) {
            if ([self determineTheTimePoint:_textArray]) {
                [self showKeyboard];
                _textView.hidden = NO;
                _showTextView.hidden = YES;
            } else {
                [JCAlertView showOneButtonWithTitle:@"提示" Message:@"在同一时间点只能添加一段文字" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
                    
                }];
//                UIAlertView *textAlert = [[UIAlertView alloc] initWithTitle:nil message:@"在同一时间点只能添加一段文字" delegate: nil cancelButtonTitle:nil otherButtonTitles: nil];
//                [textAlert show];
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [textAlert dismissWithClickedButtonIndex:0 animated:YES];
//                });
                
                _playButton.userInteractionEnabled = YES;
            }
        } else {
            [JCAlertView showOneButtonWithTitle:@"提示" Message:@"最多添加20个段文字" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
                    
                }];
//            UIAlertView *textAlert = [[UIAlertView alloc] initWithTitle:nil message:@"最多添加20个段文字" delegate: nil cancelButtonTitle:nil otherButtonTitles: nil];
//            [textAlert show];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [textAlert dismissWithClickedButtonIndex:0 animated:YES];
//            });

        }
    } else {
        [self showKeyboard];
        _textView.hidden = NO;
        _showTextView.hidden = YES;
    }
}
- (void)showKeyboard{
//    _textState = NO;
    if ([UIScreen mainScreen].bounds.size.height == 736) {
        _textView.frame = CGRectMake(0, self.view.frame.size.height - 252 - 65, self.view.frame.size.width, 40);
    } else {
        _textView.frame = CGRectMake(0, self.view.frame.size.height - 252 - 55, self.view.frame.size.width, 40);
    }
    if (_textView.textView.text.length != 0) {
        _textView.textView.text = @"";
    }
    [self.view bringSubviewToFront:_textView];
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [_textView.layer addAnimation:transition forKey:nil];
    
    [_textView.textView becomeFirstResponder];
}
- (void)textViewDidChange:(UITextView *)textView{
    [self changeTextInTextview];
//    [textView.text enumerateSubstringsInRange:NSMakeRange(0, [textView.text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
//        const unichar hs = [substring characterAtIndex:0];
//        if (0xd800 <= hs && hs <= 0xdbff) {
//            if (substring.length > 1) {
//                const unichar ls = [substring characterAtIndex:1];
//                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
//                if (0x1d000 <= uc && uc <= 0x1f77f) {
//                    [textView deleteBackward];
//                }
//            }
//        } else if (substring.length > 1) {
//            const unichar ls = [substring characterAtIndex:1];
//            if (ls == 0x20e3) {
//                [textView deleteBackward];
//            }
//        } else {
//            if (0x2100 <= hs && hs <= 0x27ff) {
//                [textView deleteBackward];
//            } else if (0x2B05 <= hs && hs <= 0x2b07) {
//                [textView deleteBackward];
//            } else if (0x2934 <= hs && hs <= 0x2935) {
//                [textView deleteBackward];
//            } else if (0x3297 <= hs && hs <= 0x3299) {
//                [textView deleteBackward];
//            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
//                [textView deleteBackward];
//            }
//        }
//    }];
}
- (void)changeTextInTextview
{
    _textView.textLength.text = [NSString stringWithFormat:@"%ld/40", _textView.textView.text.length];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18],NSFontAttributeName, nil];
    CGSize newSize = [_textView.textView.text boundingRectWithSize:CGSizeMake(_textView.textView.frame.size.width, 120) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    if (newSize.height > 40) {
        if (newSize.height <=120) {
            _textView.frame = CGRectMake(0, _textView.frame.origin.y - (newSize.height - _textView.frame.size.height + 15), self.view.frame.size.width, _textView.frame.size.height + (newSize.height - _textView.frame.size.height + 15));
        } else {
        }
    } else {
        if ([UIScreen mainScreen].bounds.size.height == 736) {
            _textView.frame = CGRectMake(0, self.view.frame.size.height - 252 - 65, self.view.frame.size.width, 40);
        } else {
            _textView.frame = CGRectMake(0, self.view.frame.size.height - 252 - 55, self.view.frame.size.width, 40);
        }
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (range.location <= 39) {
        if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
            return YES;
        } else {
            return YES;
        }
    } else {
        textView.text = [textView.text substringToIndex:40];
        if ([text isEqualToString:@""]) {
            return YES;
        } else {
            return NO;
        }
    }
}
- (void)cancelLeaveEdit{
    [_textView.textView resignFirstResponder];
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [_textView.layer addAnimation:transition forKey:nil];
    _textView.hidden = YES;
    _playButton.userInteractionEnabled = YES;
    if (_showTextView.hidden == YES && _showTextView.textLabel.text.length > 0) {
        _showTextView.hidden = NO;
    }
}
- (void)leaveEditMode{
    if (_textView.textView.text.length > 0) {
        [self.textView.textView resignFirstResponder];
        CATransition *transition = [CATransition animation];
        transition.duration = 2;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [_textView.layer addAnimation:transition forKey:nil];
        _showTextView.hidden = YES;
        [self showTextInImage:_textView.textView.text];
        [UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _textView.frame = CGRectMake(_showTextView.frame.origin.x, _showTextView.frame.origin.y, _textView.frame.size.width, _textView.frame.size.height);
            
        } completion:^(BOOL finished) {
            _textView.hidden = YES;
            _showTextView.hidden = NO;
            
        }];
        
        [self.view bringSubviewToFront:_showTextView];
//        if (_textState == YES) {
//            if (_textView.textView.text.length == 0) {
//                [_textArray removeObjectAtIndex:_xiabiao];
//                _showTextView.hidden = YES;
//            } else {
//                NSDictionary *dic = _textArray[_xiabiao];
//                NSDictionary *newDic = [NSDictionary dictionaryWithObjectsAndKeys:_textView.textView.text, @"content", [dic valueForKey:@"timeline"], @"timeline", nil];
//                [_textArray replaceObjectAtIndex:_xiabiao withObject:newDic];
//                [self showTextInImage:[_textArray[_xiabiao] valueForKey:@"content"]];
//            }
//        } else {
        if (_textView.textView.text.length != 0) {
            NSString *str = _textView.textView.text;
            textTime = [NSString stringWithFormat:@"%f", (_audioPlayer.currentTime / _audioPlayer.duration)];
            self.textDic = [NSDictionary dictionaryWithObjectsAndKeys:str, @"content", textTime, @"timeline", nil];
            if (_textArray.count > 0) {
                NSString *text_last_t = [[_textArray lastObject] valueForKey:@"timeline"];
                if (textTime.floatValue > text_last_t.floatValue) {
                    [_textArray addObject:_textDic];
                } else {
                    for (int j = 0; j < _textArray.count; j++) {
                        NSString *text_t = [_textArray[j] valueForKey:@"timeline"];
                        if (textTime.floatValue < text_t.floatValue) {
                            [_textArray insertObject:_textDic atIndex:j];
                            break;
                        }
                    }
                }
            } else {
                [_textArray addObject:_textDic];
            }
            
            NSInteger tag = 150 + textTime.floatValue * 1000;
            //显示
            [self showTextInImage:[[_textArray lastObject] valueForKey:@"content"]];
            [_addText setImage:[UIImage imageNamed:@"edit_addText_button"] forState:UIControlStateNormal];
            //波形图上添加标记
            //动画
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIImageView *markImage = [[UIImageView alloc] initWithFrame:CGRectZero];
                if ([self determineTheTimePoint:_imageArray]) {
                    markImage.frame = CGRectMake(_AudioWave.offsetNum.floatValue, 5, 12, 12);
                    markRect = markImage.frame;
                } else {
                    markImage.frame = CGRectMake(_AudioWave.offsetNum.floatValue, 12, 12, 12);
                    markRect = markImage.frame;
                }
                markImage.tag = tag;
                markImage.image = [UIImage imageNamed:@"Edit_text_mark"];
                [_waveMask addSubview:markImage];
                _publish.userInteractionEnabled = NO;
                back.userInteractionEnabled = NO;
                [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    markImage.frame = CGRectMake(markImage.frame.origin.x  - 6, markImage.frame.origin.y - 6, 24, 24);
                } completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                        markImage.frame = markRect;
                    } completion:^(BOOL finished) {
                        [UIView animateWithDuration:0.2 animations:^{
                            markImage.frame = CGRectMake(markImage.frame.origin .x - 4 , markImage.frame.origin.y - 4, 20, 20);
                        } completion:^(BOOL finished) {
                            [UIView animateWithDuration:0.4 animations:^{
                                markImage.frame = markRect;
                            } completion:^(BOOL finished) {
                                [UIView animateWithDuration:0.2 animations:^{
                                    markImage.frame = CGRectMake(markImage.frame.origin.x - 2 , markImage.frame.origin.y - 2 , 16, 16);
                                } completion:^(BOOL finished) {
                                    [UIView animateWithDuration:0.4 animations:^{
                                        markImage.frame = markRect;
                                    } completion:^(BOOL finished) {
                                        if (isEdited == 0) {
                                            [_audioPlayer play];
                                            if (_playtimer) {
                                                [_playtimer setFireDate:[NSDate distantPast]];
                                            } else {
                                                self.playtimer = [NSTimer scheduledTimerWithTimeInterval:0.08 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];
                                            }
                                            [_playButton setBackgroundImage:nil forState:UIControlStateNormal];
                                        }
                                        _publish.userInteractionEnabled = YES;
                                        back.userInteractionEnabled = YES;
                                    }];
                                }];
                            }];
                        }];
                    }];
                }];
            });
        }
//        }
        _playButton.userInteractionEnabled = YES;
    }
}
- (void)showTextInImage:(NSString *)text
{
    UIFont *font = [UIFont fontWithName:@"Arial" size:20];
    _showTextView.textLabel.font = font;
    _showTextView.textLabel.shadowColor = [UIColor blackColor];
    _showTextView.textLabel.shadowOffset = CGSizeMake(1, 1);
    _showTextView.textLabel.text = text;
    CGSize deSize = [_showTextView.textLabel sizeThatFits:CGSizeMake(self.view.frame.size.width / 3 * 2 - 10,150)];
    if (deSize.height < 40) {
        _showTextView.frame = CGRectMake(0, self.view.frame.size.width - 60, deSize.width + 20, 50);
    } else {
        _showTextView.frame = CGRectMake(0, self.view.frame.size.width - deSize.height - 20, deSize.width + 20, deSize.height + 10);
    }
    _showTextView.textLabel.frame = CGRectMake(10, 5, _showTextView.frame.size.width - 20, _showTextView.frame.size.height - 10);
}
#pragma mark -
#pragma mark 进度条
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    if (flag == YES) {
        [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
        [_playtimer invalidate];
        _playtimer = nil;
        [_AudioWave getAudioProgress:0];
        if (_imageArray.count > 0) {
            num = 0;
            textnum = 0;
            CATransition *transition = [CATransition animation];
            transition.duration = 1.0f;
            transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            transition.type = kCATransitionFade;
            [_imageView.layer addAnimation:transition forKey:nil];
            _imageView.image = [[_imageArray firstObject] valueForKey:@"image"];
        }
    }
    _showTextView.textLabel.text = @"";
    _showTextView.hidden = YES;
    _showPerson.hidden = YES;
}
- (void)audioProgress:(NSNotification *)notification{
    NSNumber *pro = notification.object;
    float progress = pro.floatValue;
    _audioPlayer.currentTime = _audioPlayer.duration * progress;
    _currentTimeLabel.text = [NSString stringWithFormat:@"%@", [self getAudioTime]];
    if (_imageArray.count > 0) {
        for (int i = 0; i < _imageArray.count; i++) {
            NSString *time = [_imageArray[i] valueForKey:@"timeline"];
            if (_audioPlayer.currentTime/_audioPlayer.duration >= time.floatValue) {
                    _imageView.image = [_imageArray[i] valueForKey:@"image"];
                    num = i;
            }
        }
    }
    if (_textArray.count != 0) {
        for (int i = 0; i < _textArray.count; i++) {
            NSString *time = [_textArray[i] valueForKey:@"timeline"];
            if (_audioPlayer.currentTime / _audioPlayer.duration >= time.floatValue) {
                CATransition *transition = [CATransition animation];
                transition.duration = 0.5;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.type = kCATransitionFade;
                [_showTextView.layer addAnimation:transition forKey:nil];
                _showTextView.hidden = NO;
                
                [self showTextInImage:[_textArray[i] valueForKey:@"content"]];
                textnum = i;
            } else {
                if (i == 0) {
                    _showTextView.hidden = YES;
                }
            }
        }
    }
}
- (void)playerPause:(NSNotification *)notification{
    if ([_audioPlayer isPlaying]) {
        [_audioPlayer pause];
        [_playtimer setFireDate:[NSDate distantFuture]];
        [_playButton setBackgroundImage:[UIImage imageNamed:@"Play_playButton"] forState:UIControlStateNormal];
    }
    NSNumber *pro = notification.object;
    float progress = pro.floatValue;
    _audioPlayer.currentTime = _audioPlayer.duration * progress;
    _currentTimeLabel.text = [NSString stringWithFormat:@"%@", [self getAudioTime]];
}
#pragma mark -
#pragma mark 获取当前播放时间
- (NSString *)getAudioTime{
    int m = _audioPlayer.currentTime / 60;
    int s = ((int) _audioPlayer.currentTime) % 60;
    //当前播放时间字符串MM:SS
    NSString *nowCurrTime=[NSString stringWithFormat:@"%.2d'%.2d\"/", m, s];
    return nowCurrTime;
}
- (BOOL)determineTheTimePoint:(NSMutableArray *)array
{
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    for (NSDictionary *dic in array) {
        NSString *time = [dic valueForKey:@"timeline"];
        [tempArray addObject:time];
    }
    NSString *textTimestr = [NSString stringWithFormat:@"%f", (_audioPlayer.currentTime / _audioPlayer.duration)];
    if (![tempArray containsObject:textTimestr]) {
        return YES;
    } else {
        return NO;
    }
}

- (void)savePhotoAndThumb{
    NSData *data;
    float kCompressionQuality = 0.8;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    for (int j = 0; j < _photoArray.count; j++) {
        UIImage *posterImage = [_photoArray objectAtIndex:j];
        data = UIImageJPEGRepresentation(posterImage, kCompressionQuality);
        [fileManager createFileAtPath:[_edit_workSpace stringByAppendingFormat:@"/full%d.png", j] contents:data attributes:nil];
    }
    if (_imageArray.count > 0) {
        NSMutableArray *timelineArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < _imageArray.count; i++) {
            NSDictionary *dic = _imageArray[i];
            NSString *timeline = [dic valueForKey:@"timeline"];
            [timelineArray addObject:timeline];
        }
        [timelineArray writeToFile:[_edit_workSpace stringByAppendingPathComponent:@"imagetimeline"]  atomically:YES];
    }
    if (_textArray.count > 0) {
        [_textArray writeToFile:[_edit_workSpace stringByAppendingPathComponent:@"text"] atomically:YES];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
